package cn.openea.perpartner.payment.web.coupon.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.openea.perpartner.common.core.utils.IdUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.payment.web.coupon.entity.Coupon;
import cn.openea.perpartner.payment.web.coupon.entity.CouponBatch;
import cn.openea.perpartner.payment.web.coupon.entity.CouponRecord;
import cn.openea.perpartner.payment.web.coupon.entity.dto.CouponDto;
import cn.openea.perpartner.payment.web.coupon.service.ICouponBatchService;
import cn.openea.perpartner.payment.web.coupon.service.ICouponRecordService;
import cn.openea.perpartner.payment.web.coupon.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "平台优惠券")
@RestController
@RequestMapping("coupon")
public class CouponController extends BaseController {

    @Autowired
    private ICouponBatchService couponBatchService;
    @Autowired
    private ICouponRecordService couponRecordService;
    @Autowired
    private ICouponService couponService;
    @ApiOperation(value = "优惠券列表", notes = "平台优惠券列表")
    @PostMapping("/list")
    public TableDataInfo selectCouponBatchList(@RequestBody CouponBatch coupon) {
        startPage();
        List<CouponBatch> list = couponBatchService.selectCouponList(coupon);
        return getDataTable(list);
    }

    @ApiOperation(value = "优惠券详情", notes = "传入优惠券ID即可")
    @PostMapping("/detail")
    public AjaxResult selectCouponBatchById(@RequestBody CouponBatch coupon) {
        return AjaxResult.success(couponBatchService.getById(coupon.getCouponId()));
    }
    @ApiOperation(value = "创建优惠券", notes = "平台创建优惠券")
    @PostMapping("/create")
    public AjaxResult saveCouponBatch(@RequestBody CouponBatch coupon) {
        if (ObjectUtil.isNull(coupon.getAging())){
            coupon.setAging(7);
        }
        coupon.setBatchId(IdUtil.nextId());
        return toAjax(couponBatchService.saveOrUpdate(coupon));
    }
    @ApiOperation(value = "发放优惠券", notes = "给用户发放优惠券")
    @PostMapping("/issue")
    public AjaxResult issueCoupon(@RequestBody Coupon coupon) {
        if (!couponBatchService.checkCouponExist(coupon.getCouponId())){
            return error("优惠券不存在！");
        }
        if (!couponBatchService.checkCouponCap(coupon.getCouponId())){
            return error("当前优惠券已无存量！");
        }
        if (!couponBatchService.checkUserCouponCap(coupon.getUserId(),coupon.getCouponId())){
            return error("用户已达当前优惠券上限！");
        }
        return toAjax(couponService.issueCoupon(coupon));
    }
    @ApiOperation(value = "创建并发放优惠券", notes = "创建并发放优惠券给用户")
    @PostMapping("/createAndIssue")
    public AjaxResult createAndIssueCoupon(@RequestBody CouponDto couponDto){
        return toAjax(couponBatchService.createAndIssueCoupon(couponDto));
    }

    @ApiOperation(value = "用户优惠券", notes = "用户优惠券列表")
    @PostMapping("/user/list")
    public TableDataInfo selectUserCouponList(@RequestBody CouponDto dto) {
        startPage();
        List<CouponDto> list = couponService.selectUserCouponList(dto);
        return getDataTable(list);
    }
    @ApiOperation(value = "使用优惠券", notes = "用户使用优惠券")
    @PostMapping("/used")
    public AjaxResult useCoupon(@RequestBody CouponRecord couponRecord) {
        if (couponBatchService.checkCouponUsed(couponRecord.getId())){
            return error("优惠券已使用或已过期！");
        }
        return toAjax(couponRecordService.useCoupon(couponRecord));
    }
    @PostMapping("/record/edit")
    @ApiOperation(value = "保存订单编号", notes = "将订单编号保存到优惠券记录中")
    public AjaxResult editRecord(@RequestBody CouponRecord couponRecord) {

        return toAjax(couponRecordService.updateById(couponRecord));
    }

    @ApiOperation(value = "统计用户优惠券", notes = "统计用户优惠券")
    @PostMapping(value = "/user/count")
    public AjaxResult getCouponCount(@RequestBody Coupon coupon) {

        return couponService.getCouponCount(coupon);
    }

}

